<!-- 出库退货审核 -->
<template>
    <div class="page-box">
        <div class="page-title">
            {{ '补库退货审核' }}
        </div>

        <div class="page-search">
            <div class="search-box" :style="{ height: IsHeight }">
                <el-input v-model="search" placeholder="采购单号/供应商/负责人" style="width: 200px; margin-right: 10px"
                    suffix-icon="search" />

            </div>
            <div class="search-btn">
                <div class="btn_item" @click="handleClickHeight">
                    <img class="btn_img" src="@/assets/sx.png" alt="" />
                    <span> 高级筛选 </span>
                </div>
            </div>
        </div>
        <div class="search-btn-right">
            <el-button color="#626aef" type="primary" @click="handleClickAdd"> 新增 </el-button>
        </div>
    </div>

    <div class="table-box">

        <el-table :data="tabeData" style="width: 100%" v-loading="loading" border>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column align="center" prop="return_order_no" label="出库退货单号" width="200" />
            <el-table-column align="center" prop="outbound_order_no" label="补库单单号" width="200" />
            <el-table-column align="center" prop="customer_name" label="客户" width="150" />
            <el-table-column align="center" prop="status" label="状态" width="100">
                <template #default="scope">
                    <el-tag :type="getStatusType(scope.row.status)" size="small">
                        {{ scope.row.status_text }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column align="center" prop="manager_name" label="退货负责人" width="120" />
            <el-table-column align="center" prop="manager_phone" label="负责人电话" width="130" />
            <el-table-column align="center" prop="creator_name" label="创建人" width="100" />
            <el-table-column align="center" prop="created_at" label="创建时间" width="160" />
            <el-table-column align="center" prop="return_date" label="退货日期" width="160" />
            <el-table-column fixed="right" label="操作" min-width="150" prop="id" align="center">
                <template #default="scope">
                    <div class="btn-box">
                        <el-button plain size="small" type="primary" @click="handleClick(scope.row)"> 审核 </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <!-- 分页 -->
    <Pagination class="pagination" @pageSize="pageSize" @getPage="getPage" :page="pageData.page"
        :page-size="pageData.pageSize" :total="total" />
    <!-- 审核弹窗 -->
    <OutboundReturnAction ref="outboundReturnActionRef" :dialogRow="dialogRow" @refresh="getAlltabeData" />
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
import { usePlatformManagementStore } from "@/stores";
import Pagination from "@/components/Pagination.vue";

import OutboundReturnAction from "./components/OutboundReturnReview/OutboundReturnAction.vue";
const platformManagementStore = usePlatformManagementStore();
const router = useRouter();
import api from "@/api";
import { ElMessage } from "element-plus";
//  新增出库
const AddedOutboundRef = ref<any>(null);
// 出库详情
let IsHeight = ref<number | string>(0);
const search = ref<string>('');
//  -----------------出库详情抽屉
//  出库详情
const DetailsOutboundOrderVisible = ref<boolean>(false);
// 定义仓库数据类型
interface Warehouse {
    id: string | number;
    title: string;
    code: string;
    house_type?: string;
    house_type_name?: string;
    category_name?: string;
    manager?: string;
    phone?: string;
    address?: string;
    remark?: string;
    parent_id?: string | number;
    parent_name?: string;
    children?: Warehouse[];
    create_time?: string;
    creator?: string;
    rowNumber?: number;
}
// 出库订单详情
let dialogRow = ref<Warehouse>({} as Warehouse);
//  -----------------列表数据
const pageData = ref({
    pageSize: 10,
    page: 1,
    status: "1",
    keywords: "",
});
const total = ref(0);
const tabeData = ref([]);
// 侦听动态分页
watch(
    () => {
        return pageData.value;
    },
    () => {
        getAlltabeData();
    },
    { deep: true }
);

let nub = ref<number>(0);
const handleClickHeight = () => {
    nub.value++;
    if (nub.value % 2 == 1) {
        IsHeight.value = '50px';
    } else {
        IsHeight.value = '0px';
    }
}

const pageSize = (e: number) => {

    pageData.value.pageSize = e
}
const getPage = (e: number) => {
    pageData.value.page = e
}
onMounted(() => {
    getAlltabeData();
})
// 获取状态文本
const getStatusText = (status: number) => {
    const statusMap: { [key: number]: string } = {
        0: '待提交',
        1: '审核中',
        2: '审核完成',
        3: '审核拒绝'
    };
    return statusMap[status] || '未知状态';
};
// 获取状态标签类型
const getStatusType = (status: number) => {
    const statusTypeMap: { [key: number]: string } = {
        0: 'info',     // 待提交
        1: 'warning',  // 审核中
        2: 'success',  // 审核完成
        3: 'danger'    // 审核拒绝
    };
    return statusTypeMap[status] || 'info';
};

// 获取审核列表
const getAlltabeData = async () => {
    loading.value = true;
    try {
        const res = await api.Outbound.getoutboundReturnList({
            ...pageData.value,
        });

        console.log('补库退货列表API返回:', res);

        if (res.code == 200) {
            // 处理数据，添加序号
            const processedData = res.data.data.map((item: any, index: number) => ({
                ...item,
                rowNumber: (pageData.value.page - 1) * pageData.value.pageSize + index + 1,
                return_amount: item.return_amount || '0.00',
                return_date: item.return_date || '--',
                customer_name: item.customer_name || '--',
                status_text: item.status_text || getStatusText(item.status),
                manager_name: item.manager_name || '--',
                manager_phone: item.manager_phone || '--',
                creator_name: item.creator_name || '--',
                created_at: item.created_at || '--',
                remark: item.remark || '--'
            }));

            tabeData.value = processedData;
            total.value = res.data.pager?.total_records || 0;
        } else {
            ElMessage.error(res.message || '获取数据失败');
        }
    } catch (error) {
        console.error('获取补库退货列表失败:', error);
        ElMessage.error('获取数据失败');
    } finally {
        loading.value = false;
    }
};

const loading = ref(false);

const outboundReturnActionRef = ref<any>(null); // 审核弹窗ref

const handleClick = async (row: any) => {
    // 请求详情接口
    const res = await api.Outbound.getoutboundReturnDetails({ id: row.id });
    if (res.code == 200) {
        outboundReturnActionRef.value.openLoading(res.data);
    } else {
        ElMessage.error(res.message || '获取详情失败');
    }
};

</script>

<style lang="scss" scoped>
.btn-box {
    display: flex;
    align-items: center;
    justify-content: center;
}

// 输入框样式
::v-deep .el-input__wrapper {
    background-color: rgba(250, 252, 251, 1);
}

//表格样式

::v-deep .el-table {
    --el-table-header-bg-color: rgba(235, 236, 240, 1);
}

::v-deep .el-table thead {
    color: #444;
}

//分页样式
.pagination {
    width: calc(100% - 250px);
    position: fixed;
    bottom: 20px;
    right: 20px;
    padding-top: 10px;
    border-top: 1px solid #98989848;


}

::v-deep .el-pagination.is-background .el-pager li.is-active {
    background-color: #000 !important;
}
</style>
